//index.js
//获取应用实例
const app = getApp()
const Url = app.url
const util = require('../../../utils/util.js')

Page({
	onShow: function () {
		this.setCustomerWxId()
		this.getMyTabCouponList()
	},

	data: {
		customerWxId: '',
		activeTab: 'a',
		show: false,
		list: [
			{
				key: 1,
				title: '未使用',
				content: [],
			},
			{
				key: 2,
				title: '已使用',
				content: [],
			},
			{
				key: 3,
				title: '已过期',
				content: [],
			},
		],
	},

	// 跳转到优惠券详情页面
	jumpToCouponQrCodePage: function (event) {
		const key = event.currentTarget.dataset.item.key
		const couponNum = event.currentTarget.dataset.o.couponNum
		const couponLogo = event.currentTarget.dataset.o.coupon.logo
		const couponName = event.currentTarget.dataset.o.coupon.name
		if (key !== 1) {
			return
		}
		wx.navigateTo({
			url: `../../home/couponQrCode/index?couponNum=${couponNum}&couponLogo=${couponLogo}&couponName=${couponName}`,
		})
	},

	// 设置openid
	setCustomerWxId: function () {
		if (!!wx.getStorageSync('openid')) {
			this.setData({
				customerWxId: wx.getStorageSync('openid'),
			})
		} else {
			wx.login({
				success: (res) => {
					wx.setStorageSync('codeMes', res.code)
					util.getOpenId(Url.login)
					this.setData({
						customerWxId: wx.getStorageSync('openid'),
					})
				},
			})
		}
	},

	// 获取我的tab优惠券列表
	getMyTabCouponList: function () {
		let that = this
		wx.showLoading({
			title: '加载中',
			mask: true,
		})

		wx.request({
			url:
				Url.myTabCouponList + `?customerWxId=${that.data.customerWxId}`,
			method: 'GET',
			data: {},
			header: {
				'content-type': 'application/json',
			},
			success: function (res) {
				wx.hideLoading()
				console.log(res, 'res')
				if (res.data.code == '200') {
					that._getResult(res.data.model)
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
				}
			},
			fail: function (err) {
				wx.hideLoading()
				console.log(err)
				wx.showToast({
					title: err.errMsg,
					icon: 'none',
					duration: 2000,
				})
			},
		})
	},

	_getResult: function (data) {
		let unUse = [],
			used = [],
			outDuring = []
		const target = [
			{
				key: 1,
				title: '未使用',
				content: unUse,
			},
			{
				key: 2,
				title: '已使用',
				content: used,
			},
			{
				key: 3,
				title: '已过期',
				content: outDuring,
			},
		]
		if (data && data.length > 0) {
			data.forEach((item) => {
				item.effectiveStartTime = item.effectiveStartTime.split(' ')[0]
				item.effectiveEndTime = item.effectiveEndTime.split(' ')[0]
				if (typeof item.coupon.config === 'string') {
					item.coupon.config = JSON.parse(item.coupon.config)
				}
				if (item.closureFlag === 1) {
					used.push(item)
				} else if (this._calcuDate(item.effectiveEndTime) >= 0) {
					outDuring.push(item)
				} else {
					unUse.push(item)
				}
			})
		}
		console.log('target', target)
		this.setData({
			list: target,
		})
	},

	_calcuDate: function (time) {
		const currentTime = new Date().getTime()
		const endTime = new Date(time).getTime()
		const diff = currentTime - endTime
		return diff
	},

	onChange: function (e) {
		this.setData({
			activeTab: e.detail.name,
		})
		if (e.detail.name === 'e') {
			this.showPopup()
		}
	},

	showPopup: function () {
		this.setData({
			show: true,
		})
	},

	onClose: function () {
		this.setData({
			show: false,
		})
	},
})
